import { baseurl } from '@/utils/config.js'
import request from '@/utils/request.js'

export function uploadFile (file) {
    return new Promise((resolve, reject) => {
        const userinfo = uni.getStorageSync('zzk:user')
        uni.uploadFile({
            url: `${baseurl}/njnzzs/upload`,
            filePath: file,
            name: 'file',
            header: {
                Authorization: userinfo.token
            },
            success: res => {
                if (res.statusCode === 200) {
                    const data = JSON.parse(res.data)
                    if (data.code == 0) {
                        let url = `${baseurl}/njnzzs/download?key=${data.data}`
                        resolve(url)
                    } else {
                        reject(false)
                    }
                } else {
                    reject(res.errMsg)
                }
            }
        })
    })
}

// download
export function downloadImg(data) {
    return request({
        url: '/njnzzs/download',
        method: 'GET',
        data
    })
}